import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Like, Repository } from 'typeorm';
import BaseKnife from '@main/entities/service/admin/base/base-knife.entity';
import { ApiException } from '@main/common/exceptions/api.exception';
import { SysOperationLogService } from '../../system/operationLog/operationLog.service';

@Injectable()
export class BaseKnifeService {
  constructor(
    @InjectRepository(BaseKnife)
    private knifeRepository: Repository<BaseKnife>,
    private operationLogService: SysOperationLogService
  ) {}

  async list() {
    return await this.knifeRepository.find();
  }

  /**
   * 罗列所有配置
   */
  async page(data) {
    const { pageSize, pageNum, entity = {} } = data;

    const { knifeName, knifeCode } = entity;

    return await this.knifeRepository.findAndCount({
      where: {
        knifeName: Like(`%${knifeName}%`),
        knifeCode: Like(`%${knifeCode}%`),
      },
      order: {
        updateTime: 'DESC',
      },
      take: pageSize,
      skip: (pageNum - 1) * pageSize,
    });
  }

  /**
   * 新增
   */
  async add(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const knife = await this.knifeRepository.insert({
      ...data,
      createName: user.nickName,
      createUserName: user.userName,
      updateName: user.nickName,
      updateUserName: user.userName,
    });
    databaseOperationLogList.push({
      type: 'add',
      dataType: 'obj',
      database: 'BaseKnife',
      checkOnlyKeyList: [],
      data: knife,
      whereObj: null,
      remark: `BaseKnife表新增数据`,
    });
    await this.operationLogService.add(
      {
        type: 'add',
        msg: `刀模管理模块新增刀模`,
        databaseOperationLogList,
        remark: `新增刀模${data.knifeName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 更新
   */
  async update(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const { id, knifeName, remark } = data;

    await this.knifeRepository.update(
      { id },
      {
        knifeName,
        remark,
        updateName: user.nickName,
        updateUserName: user.userName,
      }
    );
    databaseOperationLogList.push({
      type: 'update',
      dataType: 'obj',
      database: 'BaseKnife',
      checkOnlyKeyList: [],
      data: {
        knifeName,
        remark,
        updateName: user.nickName,
        updateUserName: user.userName,
      },
      whereObj: { id },
      remark: `BaseKnife表更新刀模信息`,
    });
    await this.operationLogService.add(
      {
        type: 'update',
        msg: `刀模管理模块更新刀模信息`,
        databaseOperationLogList,
        remark: `更新刀模${data.knifeName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 删除
   */
  async delete(idList: number[], user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    await this.knifeRepository.delete(idList);
    databaseOperationLogList.push({
      type: 'delete',
      dataType: 'array',
      database: 'BaseKnife',
      checkOnlyKeyList: [],
      data: null,
      whereObj: idList,
      remark: `BaseKnife表删除刀模`,
    });
    await this.operationLogService.add(
      {
        type: 'delete',
        msg: `刀模管理模块删除刀模`,
        databaseOperationLogList,
        remark: `删除刀模`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  async isExistKey(data) {
    const { knifeCode } = data;
    const result = await this.knifeRepository.findOne({ where: { knifeCode } });

    if (result) throw new ApiException(10053);
  }
}
